package ycl.common.base.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ycl.common.ApiResult;
import ycl.common.base.entity.BaseEntity;
import ycl.common.base.service.BaseService;
import ycl.common.models.BaseEntityRes;
import ycl.common.models.base.A;
import ycl.common.models.req.BaseReq;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 控制器基类
 *
 * @author: YCL
 * @date: 2021-06-05 00:16:44
 */
@RestController
public abstract class BaseController
		<T extends BaseEntity,
				Q extends BaseReq,
				R extends BaseEntityRes> {

	@Autowired
	@SuppressWarnings("all")
	private BaseService<T, Q, R> baseService;

	@GetMapping("pageList")
	@ApiOperation("列表分页")
	@ApiOperationSupport(order = 1)
	public abstract ApiResult<Page<R>> pageList(T t);


	@GetMapping("/getById")
	@ApiOperation("根据id获取数据")
	@ApiOperationSupport(order = 2)
	public ApiResult<T> getById(HttpServletRequest request, Long id) {
		Assert.notNull(id, "请传入id");
		return ApiResult.success(baseService.getById(id));
	}


	@PostMapping("/save")
	@ApiOperation("保存")
	@ApiOperationSupport(order = 6)
	public abstract <E extends Q> ApiResult<A> save(HttpServletRequest request, @RequestBody @Valid E e);


	@PostMapping("/deleteBatchIds")
	@ApiOperation("根据id批量删除")
	@ApiOperationSupport(order = 7)
	public ApiResult<T> deleteBatchIds(HttpServletRequest request, @RequestBody List<Long> ids) {
		baseService.removeBatchByIds(ids);
		return ApiResult.success();
	}

	@PostMapping("/update")
	@ApiOperation("修改")
	@ApiOperationSupport(order = 8)
	public abstract <E extends Q> ApiResult<A> update(HttpServletRequest request, @RequestBody @Valid E e);
}
